#include <jni.h>
#include <string>
#include "LogUtil.h"
#include "test/Test.h"
#include "render/MyGLRenderContext.h"


extern "C" JNIEXPORT jstring JNICALL
Java_com_origin_opengles_1ndk_OpenGLESActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    Test test;
    test.f1();
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}


extern "C" jint JNI_OnLoad(JavaVM *jvm, void *p) {
    LOGCATE("===== JNI_OnLoad =====");

    return JNI_VERSION_1_6;
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_origin_opengles_1ndk_A_f1(JNIEnv *env, jobject thiz) {
    return env->NewStringUTF("123");
}
extern "C"
JNIEXPORT void JNICALL
Java_com_origin_opengles_1ndk_MyNativeRender_native_1OnSurfaceCreated(JNIEnv *env, jobject thiz) {
    MyGLRenderContext::GetInstance()->OnSurfaceCreated();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_origin_opengles_1ndk_MyNativeRender_native_1OnDrawFrame(JNIEnv *env, jobject thiz) {
    MyGLRenderContext::GetInstance()->OnDrawFrame();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_origin_opengles_1ndk_MyNativeRender_native_1SetParamsInt(JNIEnv *env, jobject thiz, jint i) {
    MyGLRenderContext::GetInstance()->SetParamsInt(i);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_origin_opengles_1ndk_MyNativeRender_native_1UnInit(JNIEnv *env, jobject thiz) {
    MyGLRenderContext::DestroyInstance();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_origin_opengles_1ndk_MyNativeRender_native_1SetImageData(JNIEnv *env, jobject instance, jint format, jint width, jint height, jbyteArray imageData) {
    int len = env->GetArrayLength(imageData);
    uint8_t *buf = new uint8_t[len];
    env->GetByteArrayRegion(imageData, 0, len, reinterpret_cast<jbyte *>(buf));
    MyGLRenderContext::GetInstance()->SetImageData(format, width, height, buf);
    delete[] buf;
    env->DeleteLocalRef(imageData);
}